package com.job.DQH.dqh716.Homework;

public class CharCount {

    private Character code;
    private Integer count;

    public Character getCode() {
        return code;
    }

    public void setCode(Character code) {
        this.code = code;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public static Boolean isInLine(Character code, CharCount[] line){//判断一个新读入的字母是否在列表中

        for (int i = 0; i <line.length; i++) {
            if(line[i].code == code){
                System.out.println(code+"在列表中");
                return true;
            }
        }

        System.out.println(code+"不在列表中");

        return false;
    }

    public static CharCount[] addLine(Character code,CharCount[] line){//为未在列表中的字母加入到列表中
        CharCount[] newLine = new CharCount[line.length+1];
        for (int i = 0; i <line.length ; i++) {
            newLine[i] = line[i];
        }

        newLine[line.length] = new CharCount();
        newLine[line.length].code = code;
        newLine[line.length].count = 1;

        System.out.println(code+"已经添加进列表中");

        return newLine;
    }

    public static CharCount[] addCount(Character code,CharCount[] line){//为已经在列表中的字母count++
        for (int i = 0; i <line.length ; i++) {
            if(line[i].code == code){
                line[i].count++;
            }
        }

        System.out.println(code+"的次数已+1");

        return line;
    }

    public static CharCount[] sortLine(CharCount[] line) {//以列表的每个字母的计数排序，冒泡排序，降序排列

        CharCount temp = new CharCount();

        for (int i = 0; i < line.length - 1; i++) {
            for (int j = 0; j < line.length - 1 - i; j++) {

                if (line[j].count < line[j + 1].count) {//注意此处直接换对象不可行，why？
                    temp = line[j];
                    line[j] = line[j+1];
                    line[j+1] = temp;
                }

            }
        }

        System.out.println("列表已按照字母出现次数降序排列");
        return line;
    }

    public static void sortPrint(CharCount[] line){//降序输出数组
        int max = 0;
        int maxnum = 0;

        for (int i = 0; i <line.length ; i++) {
            for (int j = 0; j <line.length ; j++) {//每次找出count最大的元素
                if(line[j].getCount() > max){
                    max = line[j].getCount();
                    maxnum = j;
                }
            }
            if(line[maxnum].getCount()!=0)
            System.out.println(line[maxnum].getCode()+":"+line[maxnum].getCount());//输出这个count与code
            line[maxnum].setCount(0);//将其count设为零，以便下次循环是max不是它
            max = 0;
        }
        System.out.println("列表已降序输出");;
    }

}



